package com.yycx.module.bus.provider.handler.haiying.tasktodo.order;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.yycx.bpm.provider.common.handler.ProcInstDeleteHandler;
import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单流程实例删除回调
 *
 * @author zyf
 */
@Component
@DSTransactional
@Slf4j
public class HyOrderProcDeleteHandler implements ProcInstDeleteHandler {

    @Resource
    private HyOrderService orderService;

    @Resource
    private HyOrderDetailService orderDetailService;
    @Resource
    private BusinessUtils businessUtils;

    @Override
    public boolean support(String processDefinitionKey) {
        return false;
    }

    @Override
    @DSTransactional
    public ResultBody success(String businessKey, String processInstanceId) {
        HyOrder order = orderService.getById(businessKey);
        //发货状态
        Integer sendState = FlymeUtils.getInteger(order.getSendState(), 0);
        //订单状态
        Integer orderState = FlymeUtils.getInteger(order.getOrderState(), 0);
        List<HyOrderDetail> list = orderDetailService.selectByOrderId(businessKey);
        if (FlymeUtils.isNotEmpty(list)) {
            //return ResultBody.failed("请先删除订单明细");
        }
        //删除海鹰订单
        if (FlymeUtils.isNotEmpty(order)) {
            if (sendState.equals(1)) {
                /* orderService.updateOrderState(businessKey, null, CommonConstants.INT_0, CommonConstants.INT_0, 1);*/
                return ResultBody.failed("已提单禁止撤销");
            }
            //未发货
            if (sendState.equals(0)) {
                orderService.updateOrderState(businessKey, null, CommonConstants.INT_0, CommonConstants.INT_0, 0);
                if (orderState.equals(2)) {
                    businessUtils.removeRemoteOrder(order);
                }
                return ResultBody.ok();
            }

        }
        return ResultBody.ok();
    }
}
